home *** CD-ROM | disk | FTP | other *** search
- /* KillEveryBodyButMe.c */
- /* Some folks want to kill all the other applications running on a machine */
- /* for demos, school situations, kiosks, etc. */
- /* with System 7, it's easy, just use the Process Manager and AppleEvents. */
- /* This thing shows you how. */
- /* PLEASE don't abuse this. System 7 gives the user full-time multiFinder, and they */
- /* like it, our System Software team worked very hard to make this */
- /* happen. ONLY do this in very special circumstances, or you're taking */
- /* power away from the user and weaking the strength of the Mac. */
- /* Written by C.K. Haun <TR> */
- /* Apple Developer Tech Support */
-
- /* BUG FIX, if only this app and the Finder were/was running, it didn't do anything */
- /* Ooops */
-
- /* Of course, Copyright 1991-1992, Apple Computer Inc. */
- #include <Types.h>
- #include <memory.h>
- #include <Packages.h>
- #include <Errors.h>
- #include <quickdraw.h>
- #include <fonts.h>
- #include <dialogs.h>
- #include <windows.h>
- #include <menus.h>
- #include <events.h>
- #include <OSEvents.h>
- #include <Desk.h>
- #include <diskinit.h>
- #include <OSUtils.h>
- #include <resources.h>
- #include <toolutils.h>
- #include <AppleEvents.h>
- #include <EPPC.h>
- #include <GestaltEqu.h>
- #include <PPCToolbox.h>
- #include <Processes.h>
- #include <Balloons.h>
- /* prototypes */
- void InitalizeApp(void);
- void DoDiskEvents(long dinfo); /* hi word is error code, lo word is drive number */
- void DrawMain(WindowPtr drawIt);
- Boolean DoSelected(long val);
-
- void InitAEStuff(void);
- void DoHighLevel(EventRecord *AERecord);
- void DoDaCall(MenuHandle themenu, long theit);
- pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
- pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
- pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
- pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
- void SampleHelpDialog(void);
- void KillEveryBody(void);
- /* one external */
- extern void _DataInit(); /* this is the C initialization code */
-
- #define kMBarID 128
- #define kAppleMenu 128
- #define kFileMenu 129
- #define kEditMenu 130
- #define kToolsMenu 131
- #define kResumeMask 1 /* bit of message field for resume vs. suspend */
- #define kSampHelp 129
- #define kAboutBox 128
- #define kHelpString 128
- #define kNewItem 1
- #define kOpenItem 2
- #define kCloseItem 3
- #define kSaveItem 4
- #define kSaveAsItem 5
- #define kFileBlank1 6
- #define kPageSetupItem 7
- #define kPrintItem 8
- #define kFileBlank2 9
- #define kQuitItem 10
- #define kBadSystem 130
- #define kTitle 129
-
- Handle gMymenu; /* my menu bar handle */
- MenuHandle gAppleMenuHandle, gFileMenuHandle, gEditMenuHandle, gToolMenuHandle;
- Boolean gQuit, gInBackground;
- EventRecord gERecord;
- AEDesc gTheAddress;
- WindowPtr myWindow;
- ProcessSerialNumber gOurSN;
- short gHelpItem;
-
- #ifdef powerc
- QDGlobals qd;
- #endif
-
- #pragma segment Main
- main()
- {
- WindowPtr twindow;
- UnloadSeg((Ptr)_DataInit); /* throw out setup code */
- InitalizeApp();
- UnloadSeg((Ptr)InitalizeApp); /* get rid of my initialization code */
- do {
- WaitNextEvent(everyEvent, &gERecord, 30, nil);
- switch (gERecord.what) {
-
- case nullEvent:
- /* no nul processing in this sample */
- break;
- case updateEvt:
- DrawMain((WindowPtr)gERecord.message); /* draw whatever window needs an update */
- break;
- case mouseDown:
- /* first see where the hit was */
- switch (FindWindow(gERecord.where, &twindow)) {
-
- case inDesk: /* if they hit in desk, then the process manager */
- break; /* will switch us out, we don't need to do anything */
- case inMenuBar:
- DoSelected(MenuSelect(gERecord.where));
- break;
-
- case inSysWindow:
- /* pass to the system */
- SystemClick(&gERecord, twindow);
- break;
- case inContent:
- /* Handle content and control clicks here */
- break;
- case inDrag:
- if (twindow == FrontWindow())
- DragWindow(twindow, gERecord.where, &qd.screenBits.bounds);
- break;
- case inGrow:
- /* Call GrowWindow here if you have a grow box */
- break;
- case inGoAway:
- /* Click in Close box */
- break;
-
- }
- case mouseUp:
- /* don't care */
- break;
- /* same action for key or auto key */
- case keyDown:
- case autoKey:
- if (gERecord.modifiers & cmdKey)
- DoSelected(MenuKey(gERecord.message & charCodeMask));
- break;
- case keyUp:
- /* don't care */
- break;
- case diskEvt:
- /* I don't do anything special for disk events, this just passes them */
- /* to a function that checks for an error on the mount */
- DoDiskEvents(gERecord.message);
- break;
- case activateEvt:
- if (gERecord.modifiers & activeFlag){
- SetPort(myWindow);
- DrawMain((WindowPtr)gERecord.message);
- }
- break;
- case networkEvt:
- /* don't care */
- break;
- case driverEvt:
- /* don't care */
- break;
- case app4Evt:
- switch ((gERecord.message >> 24) & 0x0FF) { /* high byte of message */
- case suspendResumeMessage: /* suspend/resume is also an activate/deactivate */
- gInBackground = (gERecord.message & kResumeMask) == 0;
- break;
- }
- break;
- default:
- break;
- /* This dispatches high level events (AppleEvents, for example) */
- /* to our dispatch routine. This is NEW in the event loop for */
- /* System 7 */
- case kHighLevelEvent:
- DoHighLevel(&gERecord);
- break;
-
- }
- }
- while (gQuit != true);
-
- }
-
- /* DoDaCall opens the requested DA. It's here as a seperate routine if you'd */
- /* like to perform some action or just know when a DA is opened in your */
- /* layer. Can be handy to track memory problems when a DA is opened */
- /* with an Option-open */
- void DoDaCall(MenuHandle themenu, long theit)
- {
- long qq;
- char DAname[255];
- GetItem(themenu, theit, &DAname);
- qq = OpenDeskAcc(DAname);
- }
-
- /* end DoDaCall */
-
- /* DoDiskEvents just checks the error code from the disk mount, */
- /* and puts up the 'Format' dialog (through DIBadMount) if need be */
- /* You can do much more here if you care about what disks are */
- /* in the drive */
- void DoDiskEvents(long dinfo) /* hi word is error code, lo word is drive number */
- {
- short hival, loval, tommy;
- Point fredpoint = {
- 40, 40
- };
- hival = HiWord(dinfo);
- loval = LoWord(dinfo);
- if (hival != noErr) /* something happened */ {
- tommy = DIBadMount(fredpoint, dinfo);
- }
- }
-
- /* draws my window. Pretty simple */
- void DrawMain(WindowPtr drawIt)
- {
- short vPos, hPos;
- short incre = 20;
- short oldFace;
- ProcessInfoRec infoRec;
- StringHandle theString;
- OSErr myErr;
- Str31 processName;
- FSSpec procSpec;
- ProcessSerialNumber processSN;
- processSN.lowLongOfPSN = kNoProcess;
- processSN.highLongOfPSN = kNoProcess;
- vPos = 15;
- hPos = 20;
- BeginUpdate(drawIt);
- SetPort(drawIt);
- EraseRect(&drawIt->portRect);
- MoveTo(hPos, vPos);
- theString = GetString(kTitle);
- HLock((Handle)theString);
- oldFace = drawIt->txFace;
- TextFace(bold);
- DrawString((ConstStr255Param)*theString);
- TextFace(oldFace);
- ReleaseResource((Handle)theString);
- vPos += incre;
- do {
- myErr = GetNextProcess(&processSN);
-
- infoRec.processInfoLength = sizeof(ProcessInfoRec);
- infoRec.processName = &processName;
- infoRec.processAppSpec = &procSpec;
- myErr = GetProcessInformation(&processSN, &infoRec);
- if (!myErr) {
- MoveTo(hPos, vPos);
- DrawString(infoRec.processName);
- vPos += incre;
- }
- }
- while (myErr == noErr);
-
- EndUpdate(drawIt);
- }
-
- /* my menu action taker. It returns a Boolean which I usually ignore, but it */
- /* mught be handy someday */
- Boolean DoSelected(long val)
- {
- short loval, hival;
- Boolean returnVal = false;
- loval = LoWord(val);
- hival = HiWord(val);
-
- switch (hival) { /* switch off the menu number selected */
- case kAppleMenu: /* Apple menu */
- if (loval != 1) { /* if this was not About, it's a DA */
- DoDaCall(gAppleMenuHandle, loval);
- } else {
- Alert(kAboutBox, nil); /* do about box */
- }
- returnVal = true;
- break;
- case kFileMenu: /* File menu */
- switch (loval) {
- case kQuitItem:
- gQuit = true; /* only item */
- returnVal = true;
- break;
- default:
- break;
- }
- break;
- case kEditMenu:
- /* edit menu junk */
- /* don't care */
- break;
- case kToolsMenu:
- /* add all your test stuff here */
- /* only one item, kill stuff */
- KillEveryBody();
- InvalRect(&myWindow->portRect);
- break;
- case kHMHelpMenuID: /* Defined in Balloons.h */
- /* I only care about this item. If anything else is returned here, I don't know what */
- /* it is, so I leave it alone. Remember, the Help Manager chapter says that */
- /* Apple reserves the right to add and change things in the Help menu */
- if (loval == gHelpItem)
- SampleHelpDialog();
- break;
-
- }
- HiliteMenu(0);
- return(returnVal);
- }
-
- /* InitAEStuff installs my appleevent handlers */
- void InitAEStuff(void)
- {
- OSErr aevtErr = noErr;
- long aLong = 0;
- Boolean gHasAppleEvents = false;
- /* Check this machine for AppleEvents. If they are not here (ie not 7.0)
- * then we exit */
- gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &aLong) == noErr);
- /* The following series of calls installs all our AppleEvent Handlers.
- * These handlers are added to the application event handler list that
- * the AppleEvent manager maintains. So, whenever an AppleEvent happens
- * and we call AEProcessEvent, the AppleEvent manager will check our
- * list of handlers and dispatch to it if there is one.
- */
- if (gHasAppleEvents) {
- aevtErr = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
- NewAEEventHandlerProc(AEOpenHandler),0, false);
- if (aevtErr) ExitToShell();
-
- aevtErr = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
- NewAEEventHandlerProc(AEOpenDocHandler),0, false);
- if (aevtErr) ExitToShell();
-
- aevtErr = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
- NewAEEventHandlerProc(AEQuitHandler), 0, false);
- if (aevtErr) ExitToShell();
-
- aevtErr = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
- NewAEEventHandlerProc(AEPrintHandler),0, false);
- if (aevtErr) ExitToShell();
-
- if (aevtErr) ExitToShell();
-
- }
- else ExitToShell();
-
- }
- /* end InitAEStuff */
-
-
-
- /* I'm not doing error handling in this sample for clarities sake, you should. Hah, */
- /* easy for me to say, huh? */
- void DoHighLevel(EventRecord *AERecord)
- {
-
- AEProcessAppleEvent(AERecord);
-
- }
-
- /* end DoHighLevel */
-
- /* This is the standard Open Application event. */
- pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
- {
- #pragma unused (messagein,reply,refIn)
- /* we of course don't do anything here in this simple app */
- /* except open our window */
- myWindow = GetNewWindow(128, nil, (WindowPtr)-1);
- SetPort(myWindow);
- return(noErr);
- }
-
- /* end AEOpenHandler */
-
- /* Open Doc, opens our documents. Remember, this can happen at application start AND */
- /* anytime else. If your app is up and running and the user goes to the desktop, hilites one */
- /* of your files, and double-clicks or selects Open from the finder File menu this event */
- /* handler will get called. Which means you don't do any initialization of globals here, or */
- /* anything else except open then doc. */
- /* SO-- Do NOT assume that you are at app start time in this */
- /* routine, or bad things will surely happen to you. */
-
- pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
- {
- #pragma unused (messagein,refIn,reply)
- /* we of course don't do anything here */
- return(errAEEventNotHandled); /* we have no docs, so no odoc events should come to us */
- }
-
- pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
- { /* no printing handler in yet, so we'll ignore this */
- /* the operation is functionally identical to the ODOC event, with the additon */
- /* of calling your print routine. */
- #pragma unused (messagein,refIn,reply)
- /* we of course don't do anything here */
- return(errAEEventNotHandled); /* we have no docs, so no pdoc events should come to us */
- }
-
- /* Standard Quit event handler, to handle a Quit event from the Finder, for example. */
- /* ••••• DO NOT CALL EXITTOSHELL HERE ••••• or you will never have a happy life. */
- pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
- {
- #pragma unused (messagein,refIn,reply)
-
- /* prepQuit sets the Stop flag for us. It does _NOT_ quit, you */
- /* should NEVER quit from an AppleEvent handler. Calling */
- /* ExitToShell here would blow things up */
- gQuit = true;
- return(noErr);
- }
-
-
- /* This is my sample help dialog. Does not do anything, expand as you need */
- void SampleHelpDialog(void)
- {
- DialogPtr tdial = GetNewDialog(kSampHelp, nil, (WindowPtr)-1);
- short itemhit = 0;
- while (itemhit != 1) {
- ModalDialog(nil, &itemhit);
- }
- DisposDialog(tdial);
- }
-
-
- /* This is the killer code. It finds and kills every other */
- /* application on your machine. */
- void KillEveryBody(void)
- {
- ProcessSerialNumber myProc, processSN;
- ProcessSerialNumber finderPSN;
- ProcessInfoRec infoRec;
- Str31 processName;
- FSSpec procSpec;
-
- OSErr myErr = noErr;
- OSErr otherError;
- AppleEvent theEvent;
- AEDesc theAddress;
- Boolean ourFlag, notFinder;
- Boolean finderFound = false;
- GetCurrentProcess(&myProc);
- /* Preset the PSN to no PSN, see IM VI, the Process Manager */
- processSN.lowLongOfPSN = kNoProcess;
- processSN.highLongOfPSN = kNoProcess;
- finderPSN.lowLongOfPSN = nil;
- finderPSN.highLongOfPSN = nil;
-
- do {
- myErr = GetNextProcess(&processSN);
- /* See if it's us first */
- notFinder = true;
- SameProcess(&myProc, &processSN, &ourFlag);
-
- infoRec.processInfoLength = sizeof(ProcessInfoRec);
- infoRec.processName = &processName;
- infoRec.processAppSpec = &procSpec;
- GetProcessInformation(&processSN, &infoRec);
- if (!ourFlag && !finderFound) {
- /* see if it's the Finder, we have to kill the finder LAST */
- /* or else non-sys 7 apps won't get killed */
- /* since the Finder must be there to convert the AppleEvent to Puppet Strings */
- /* if the app is not APpleEvent aware */
- /* Also, FileShare HAS to be killed before the Finder */
- /* or your life will be unpleasant */
-
- if (infoRec.processSignature == 'MACS' && infoRec.processType == 'FNDR') {
- /* save this number for later */
- finderPSN = processSN;
- notFinder = false;
- finderFound = true;
-
- } else {
- notFinder = true;
- }
- }
- if (!myErr && !ourFlag && notFinder) {
- otherError = AECreateDesc(typeProcessSerialNumber, (Ptr)&processSN, sizeof(processSN), &theAddress);
- if (!otherError)
- otherError = AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, &theAddress, kAutoGenerateReturnID,
- kAnyTransactionID, &theEvent);
- if (!otherError)
- AEDisposeDesc(&theAddress);
- /* Again, the Finder will convert the AppleEvent to puppetstrings if */
- /* the application is a System 6 or non-AE aware app. This ONLY */
- /* happens for the 4 required (oapp,odoc,pdoc, and quit) AppleEvents */
- /* and ONLY if you use the PSN for the address */
- if (!otherError)
- AESend(&theEvent, nil, kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout,
- nil, nil);
- AEDisposeDesc(&theEvent);
- }
- } while (!myErr);
-
- /* Now, if the finder was running, it's safe to kill it */
- if (finderPSN.lowLongOfPSN || finderPSN.highLongOfPSN) {
- otherError = AECreateDesc(typeProcessSerialNumber, (Ptr)&finderPSN, sizeof(processSN), &theAddress);
- if (!otherError)
- otherError = AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, &theAddress, kAutoGenerateReturnID,
- kAnyTransactionID, &theEvent);
- if (!otherError)
- AEDisposeDesc(&theAddress);
- if (!otherError)
- AESend(&theEvent, nil, kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, nil,
- nil);
- AEDisposeDesc(&theEvent);
- }
- }
-
-
-
- #pragma segment Initialize
- void InitalizeApp(void)
- {
- MenuHandle helpHandle;
- StringHandle helpString;
- short count;
- long vers;
- MaxApplZone();
- InitGraf((Ptr)&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
- /* Check system version */
- Gestalt(gestaltSystemVersion, &vers);
- vers = (vers >> 8) & 0xf; /* shift result over and mask out major version number */
- if (vers < 7) {
- StopAlert(kBadSystem, nil);
- ExitToShell();
- }
- InitAEStuff();
- /* set up my menu junk */
- gMymenu = GetNewMBar(kMBarID);
- SetMenuBar(gMymenu);
- gAppleMenuHandle = GetMHandle(kAppleMenu);
- gFileMenuHandle = GetMHandle(kFileMenu);
- gEditMenuHandle = GetMHandle(kEditMenu);
- gToolMenuHandle = GetMHandle(kToolsMenu);
- AddResMenu(gAppleMenuHandle, 'DRVR');
- /* now install my Help menu item in the Help Manager's menu */
- HMGetHelpMenuHandle(&helpHandle); /* Get the Hlpe menu handle */
- count = CountMItems(helpHandle); /* How many items are there? */
- helpString = GetString(kHelpString); /* get my help string */
- DetachResource(helpString); /* detach it */
- HNoPurge(helpString);
- MoveHHi((Handle)helpString);
- HLock((Handle)helpString);
- InsMenuItem(helpHandle, (Ptr)*helpString, count + 1); /* insert my item in the Help menu */
- gHelpItem = CountMItems(helpHandle); /* The number of the item */
-
- DrawMenuBar();
- GetCurrentProcess(&gOurSN); /* Get our process serial number for later use, if needed */
-
- }
-